package com.avicit.zipkinkafkaesserver.dao.es;

import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;

import java.io.IOException;
import java.util.Map;


/**
 * ElasticsearchTemplate 包装
 */
public class ElasticsearchTemplateWarpper {

    private  static ElasticsearchTemplate elasticsearchTemplate;

    private static Builder builder;

    public static class Builder {
        /**
         * 创建 index
         * @param index
         * @return
         */
      public boolean createIndex(String index) {
          if (!elasticsearchTemplate.indexExists(index)) {
              return elasticsearchTemplate.createIndex(index);
          }
          return true;
      }
      public boolean createIndex(String index, String type ,XContentBuilder mapping) {
          if (!elasticsearchTemplate.indexExists(index)) {
              elasticsearchTemplate.createIndex(index);
              boolean putMapping = elasticsearchTemplate.putMapping(index, type, mapping);
              return putMapping;
          }
              boolean putMapping = elasticsearchTemplate.putMapping(index, type, mapping);
              return putMapping;
          }
      }

    public static Builder builder(ElasticsearchTemplate es) {
        elasticsearchTemplate = es;
        if (builder == null) {
            synchronized (Builder.class) {
                if (builder == null) {
                    builder = new Builder();
                }
            }
        }
        return builder;
    }
}
